package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    //处理查询所有的请求

    @RequiredPermission(name = "员工列表",expression = "employee.list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject queryObject){
        PageInfo<Employee> pageInfo = employeeService.query(queryObject);
        model.addAttribute("pageInfo",pageInfo);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        return "/employee/list";
    }
    @RequiredPermission(name="员工删除",expression = "employee.delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }
    @RequiredPermission(name="员工新增",expression = "employee.input")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.listAll();
        // 查询所有部门往模型存入，提供数据，让员工新增修改时显示出来以供用户选择
        model.addAttribute("departments", departments);
        model.addAttribute("role",roleService.selectAll());
        if(id != null){
            model.addAttribute("selfRole",roleService.selectByEmpId(id));
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/input";
    }

    @RequiredPermission(name="员工编辑",expression = "employee.saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if(employee.getId()==null){
            employeeService.save(employee,ids);
        }else{
            employeeService.update(employee,ids);
        }
        return "redirect:/employee/list";
    }
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        String fileName = new String("员工表单.xls".getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        employeeService.exportXls(response.getOutputStream());
    }
    @RequestMapping("importXls")
    public String importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }
}
